<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-date-picker
        v-model="screenDate"
        type="date"
        placeholder="选择日期"
        value-format="yyyy-MM-dd"
        @change="dateChange"/>
        <span>轮数：</span>
      <el-select v-model="recordNumber" placeholder="请选择" @change="recordChange">
        <el-option
          v-for="item in count"
          :key="item"
          :label="item"
          :value="item"/>
      </el-select>
      <el-button v-permission="['PatrolRecord:inspectionrounds:list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <!-- <el-button v-permission="['PatrolRecord:inspectionrounds:create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button> -->
    </div>
    <div class="info_top">第 {{recordNumber}} 轮  <span style="margin-left:10px">巡检人 {{persion?persion:"暂无"}}</span> <span style="margin-left:10px">{{patrolDate}}</span> </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      size="small"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
    >
      <el-table-column type="index" align="center" label="序号" width="50">
        <template scope="scope">
          <span v-if="scope.$index < 9">0{{ scope.$index + 1 }}</span>
          <span v-else>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="所属机房" prop="roomName" />
      <el-table-column align="center" label="设备类型" prop="equipmentType" />
      <el-table-column align="center" label="设备编号" prop="equipmentNo" />
      <el-table-column align="center" label="温度（℃）" prop="temperature" />
      <el-table-column align="center" label="湿度（%）" prop="humidity" />
      <el-table-column align="center" label="运行状态" prop="runningStatus" >
        <template scope="scope">
          <!-- {{ scope.row.runningStatus == 1?"正常":"异常" }} -->
           <div v-if="scope.row.runningStatus == 1" style="width:100%;background-color: #3ebc37;color:#ffffff;">正常</div>
           <div v-if="scope.row.runningStatus == 2" style="width:100%;background-color: #f0541d;color:#ffffff;">异常</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="巡检结果" prop="abnormalMatters" >
         <template scope="scope">
           <div v-if="scope.row.inspectionStatus == 3" style="width:100%;background-color: #3ebc37;color:#ffffff;">正常</div>
           <div v-if="scope.row.inspectionStatus == 4" style="width:100%;background-color: #f0541d;color:#ffffff;">异常</div>
           <div v-if="scope.row.inspectionStatus == 5" style="width:100%;background-color: #999999;color:#ffffff;">漏检</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="附件" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button v-permission="['PatrolRecord:inspectionrounds:edit']" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="['PatrolRecord:inspectionrounds:delete']" type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button> -->
          <el-button v-permission="['PatrolRecord:inspectionrounds:edit']" type="primary" size="mini" @click="enlarge(scope.row)">查看</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="id" prop="id" hidden>
          <el-input v-model="dataForm.id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" :loading="submiting" type="primary" @click="createData">确定</el-button>
        <el-button v-else :loading="submiting" type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
    <!-- <el-dialog :visible.sync="dialogVisible" :before-close="handleClose" title="附件">
      <el-carousel :interval="5000" arrow="always" style="width:100%" height="500px">
        <el-carousel-item v-for="item in srcList" :key="item">
          <div style="margin:0 auto">
            <img :src="item" mode="aspectFit" alt="" style="width:100%;height:500px"/>
          </div>
        </el-carousel-item>
      </el-carousel>
    </el-dialog> -->
    <el-image-viewer v-if="dialogVisible" :on-close="closeImgViewer" :url-list="srcList" style="z-index:9999" />
  </div>
</template>

<script>
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
import {
  createInspectionRounds,
  updateInspectionRounds,
  deleteInspectionRounds,
  roundNumber,
  getRecordList
} from '@/api/record'
import Pagination from '@/components/Pagination'
// import imageViewer from '../../../node_modules/element-ui/packages/image/src/image-viewer.vue'
export default {
  name: 'InspectionRounds',
  components: {
    Pagination,
    ElImageViewer 
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      dataForm: {
        id: undefined
      },
      dialogFormVisible: false,
      submiting: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
      },
      screenDate: '', // 日期
      recordInfo: [],
      count: 1,
      recordNumber: 1,
      // 图片预览
      srcList: [],
      // forbiddenshow:false,
      // imgsVisible: false,
      // disabledDays:''
      dialogVisible: false,
      persion:"",//巡检人
      patrolDate:"",//巡检时间
    }
  },
  created() {
    this.getDate()
    this.getList()
  },
  methods: {
    
      closeImgViewer() {
         this.srcList = []
        this.dialogVisible = false;
      },
    enlarge(val) {
      this.dialogVisible = true
      // val.photo = "https://taohaoliang.oss-cn-beijing.aliyuncs.com/admin/195c8e3d3035404bb8cdbe176949076c.jpg,https://taohaoliang.oss-cn-beijing.aliyuncs.com/admin/1d9d101513914135b19295ce68b1ed17.jpg"
      this.srcList = val.photo.split(',')
    },
    dateChange(e) {
      this.recordNumber = 1
      this.getList()
    },
    handleClose() {
      this.srcList = []
      this.dialogVisible = false
    },
    getDate() {
      const date = new Date()
      const year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      if (month < 10) {
        month = '0' + month
      }
      if (day < 10) {
        day = '0' + day
      }
      this.screenDate = year + '-' + month + '-' + day
    },
    recordChange(e) {
      this.recordNumber = e
      this.listQuery.roundsId = this.recordInfo[e - 1].id
      this.getList()
    },
    lookInfo(val) { // 查看

    },
    getList() {
      this.listLoading = true
      roundNumber({ date: this.screenDate }).then(response => {
        if (response.data.data.length > 0) {
          for(let i = 0 ; i < response.data.data.length;i++){
              if(this.recordNumber == response.data.data[i].rounds){
                   this.listQuery.roundsId = response.data.data[i].id
                    this.persion = response.data.data[i].inspectionPeople
                    this.patrolDate = response.data.data[i].inspectionStart.split(" ")[1] +" ~ " + response.data.data[i].inspectionEnd.split(" ")[1]
              }
          }
          this.recordInfo = response.data.data
          this.count = response.data.data.length
          this.getNext()
        } else {
          this.count = 1
          this.list = []
          this.persion = ""
          this.patrolDate = ""
        }
        this.listLoading = false
      }).catch(() => {})
    },
    getNext() {
      this.listLoading = true
      getRecordList(this.listQuery).then(response => {
        this.list = response.data.data.items
        this.total = response.data.data.total
        this.listLoading = false
      })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.submiting = true
          createInspectionRounds(this.dataForm).then(response => {
            this.list.unshift(response.data.data)
            this.dialogFormVisible = false
            this.$notify.success({
              title: '成功',
              message: '添加成功'
            })
            this.submiting = false
          })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg
              })
              this.submiting = false
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.submiting = true
          updateInspectionRounds(this.dataForm).then(() => {
            for (const v of this.list) {
              if (v.id === this.dataForm.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.dataForm)
                break
              }
            }
            this.dialogFormVisible = false
            this.submiting = false
            this.$notify.success({
              title: '成功',
              message: '更新成功'
            })
          })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg
              })
              this.submiting = false
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该记录---' + row.id + '---, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteInspectionRounds(row.id).then(response => {
          this.$notify.success({
            title: '成功',
            message: '删除成功'
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.errmsg
            })
          })
      }).catch(() => {
        return false
      })
    }
  }
}
</script>
<style scoped>
.info_top{
  margin: 20px 3px;
}

</style>
